//
//  UIButton+YLAdditions.m
//  yuLink
//
//  Created by aiwoba on 2021/3/5.
//

#import "UIButton+YLAdditions.h"

@implementation UIButton (YLAdditions)
static char *actionBlockKey;
+ (instancetype)buttonWithType:(UIButtonType)buttonType configure:(void(^)(UIButton *btn))configureBlock action:(void(^)(UIButton *btn))actionBlock {
    UIButton *btn = [UIButton buttonWithType:buttonType];
    if (configureBlock) {
        configureBlock(btn);
    }
    btn.titleLabel.adjustsFontSizeToFitWidth = YES;
    objc_setAssociatedObject(btn, &actionBlockKey, actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [btn addTarget:btn action:@selector(actionCallBlock:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}
#pragma mark -actionCallBlock
- (void)actionCallBlock:(UIButton *)btn {
    void (^actionBlock) (UIButton *btn) = objc_getAssociatedObject(self, &actionBlockKey);
    if (actionBlock) {
        actionBlock(btn);
    }
}

@end
